package netty.pipeline;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test;

/**
 * Instruction：
 * 测试
 * Author：@author MaLi
 */
public class PipeLineDemo {
    @Test
    public void testPipeLineInBound() {
        ChannelInitializer<EmbeddedChannel> initializer = new ChannelInitializer<>(){
            @Override
            protected void initChannel(EmbeddedChannel ch) throws Exception {
                ch.pipeline().addLast(new SimpleInHandlerA());
                ch.pipeline().addLast(new SimpleInHandlerB());
                ch.pipeline().addLast(new SimpleInHandlerC());
            }
        };
        //创建一个Channel
        EmbeddedChannel channel = new EmbeddedChannel(initializer);
        ByteBuf buffer = Unpooled.buffer();
        buffer.writeInt(1);
        // Channel
        channel.writeInbound(buffer);
    }

    @Test
    public void testPipeLineOutBound() {
        //出站处理器的执行顺序和加入顺序相反
        ChannelInitializer<EmbeddedChannel> initializer = new ChannelInitializer<>(){
            @Override
            protected void initChannel(EmbeddedChannel ch) throws Exception {
                ch.pipeline().addLast(new SimpleInHandlerE());
                ch.pipeline().addLast(new SimpleInHandlerF());
                ch.pipeline().addLast(new SimpleInHandlerG());
            }
        };
        //创建一个Channel
        EmbeddedChannel channel = new EmbeddedChannel(initializer);
        ByteBuf buffer = Unpooled.buffer();
        buffer.writeInt(1);
        // Channel
        channel.writeOutbound(buffer);
    }

    public static class SimpleInHandlerA extends ChannelInboundHandlerAdapter{
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            super.channelRead(ctx, msg);
            System.out.println("SimpleInHandlerA 被回调");

        }
    }

    public static class SimpleInHandlerB extends ChannelInboundHandlerAdapter{
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("SimpleInHandlerB 被回调");
            super.channelRead(ctx, msg);
        }
    }

    public static class SimpleInHandlerC extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("SimpleInHandlerC 被回调");
            super.channelRead(ctx, msg);
        }
    }

    public static class SimpleInHandlerE extends ChannelOutboundHandlerAdapter {
        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("SimpleInHandlerE 被回调");
            super.write(ctx, msg, promise);
        }
    }

    public static class SimpleInHandlerF extends ChannelOutboundHandlerAdapter {
        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("SimpleInHandlerF 被回调");
            super.write(ctx, msg, promise);
        }
    }

    public static class SimpleInHandlerG extends ChannelOutboundHandlerAdapter {
        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("SimpleInHandlerG 被回调");
            // 该行代码类似放行, 决定先处理后面的handler还是当前Handler
            super.write(ctx, msg, promise);

        }
    }
}
